/*
 * SelectChatObjectActivity.java
 * Author:  lixh
 * Create Date:  2015年6月28日
 */
package com.yinhai.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.listener.MsgTextLongClickListener;
import com.yinhai.adapter.ContactAdpter;
import com.yinhai.constant.ChatConstant;
import com.yinhai.constant.Contact;
import com.yinhai.model.MsgModel;
import com.yinhai.model.UserUserContact;
import com.yinhai.service.ContactUserServiceImpl;

/**
 * SelectChatObjectActivity (聊天选择界面)
 *
 * Author:lixh
 */
public class SelectChatObjectActivity extends Activity {
    private List<UserUserContact> datas = new ArrayList<UserUserContact>();
    
    private ContactAdpter contactAdpter;
    
    private ListView mListView;
    
    private Stack<String> parentStack = new Stack<String>();
    
    private List<UserUserContact> rootDatas = new ArrayList<UserUserContact>();
    
    private ContactUserServiceImpl contactUserServiceImpl;
    
    private MsgModel resendMsgModel;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_chat_object);
        contactUserServiceImpl = new ContactUserServiceImpl(this);
        contactAdpter = new ContactAdpter(this, datas);
        if (getIntent().hasExtra(MsgTextLongClickListener.SER_KEY)) {
            resendMsgModel = (MsgModel)getIntent().getSerializableExtra(MsgTextLongClickListener.SER_KEY);
        }
        mListView = (ListView)this.findViewById(R.id.listview);
        mListView.setAdapter(contactAdpter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                UserUserContact userContact = datas.get(arg2);
                if (userContact.getType().equals(Contact.CONTACT_TYPE_GROUP)) {
                    if (userContact.getId().equals("-1")) {
                        Intent intent = new Intent(SelectChatObjectActivity.this, ChatGroupActivity.class);
                        if (resendMsgModel != null) {
                            Bundle mBundle = new Bundle();
                            mBundle.putSerializable(MsgTextLongClickListener.SER_KEY, resendMsgModel);
                            intent.putExtras(mBundle);
                        }
                        startActivity(intent);
                        finish();
                    } else {
                        datas.clear();
                        datas.addAll(findContact(userContact.getId()));
                        contactAdpter.notifyDataSetChanged();
                    }
                    
                } else {
                    
                    Intent intent = new Intent(SelectChatObjectActivity.this, ChatActivity.class);
                    intent.putExtra("receID", userContact.getPhone());
                    intent.putExtra("receName", userContact.getName());
                    intent.putExtra("type", 1);// 1:一对一；2：部门；3：群组
                    ChatConstant.CURRENT_CHAT = "1";
                    ChatConstant.CURRENT_CHART_ID = userContact.getPhone();
                    if (resendMsgModel != null) {
                        Bundle mBundle = new Bundle();
                        mBundle.putSerializable(MsgTextLongClickListener.SER_KEY, resendMsgModel);
                        intent.putExtras(mBundle);
                    }
                    startActivity(intent);
                    finish();
                }
                
            }
        });
        initData();
    }
    
    private void initData() {
        UserUserContact userContact = new UserUserContact();
        userContact.setId("-1");
        userContact.setName("发起群聊");
        userContact.setNum("");
        userContact.setType(Contact.CONTACT_TYPE_GROUP);
        userContact.setMatchType(Contact.MATCH_NAME);
        rootDatas.add(userContact);
        List<UserUserContact> contacts = findContact("0");
        for (UserUserContact contact : contacts) {
            // 移出本地通讯录
            if (contact.getId().equals("groupId")) {
                contacts.remove(contact);
                break;
            }
        }
        rootDatas.addAll(contacts);
        datas.clear();
        datas.addAll(rootDatas);
        contactAdpter.notifyDataSetChanged();
        
    }
    
    private List<UserUserContact> findContact(String parentID) {
        List<UserUserContact> contacts = new ArrayList<UserUserContact>();
        parentStack.push(parentID);
        contacts.addAll(contactUserServiceImpl.searchNewGroup(null, parentID, null));
        contacts.addAll(contactUserServiceImpl.searchContactUser(null, parentID, null));
        return contacts;
    }
    
    public void doBack(View view) {
        parentStack.pop();
        if (parentStack.size() == 0) {
            onBackPressed();
        } else {
            datas.clear();
            String parentID = parentStack.pop();
            if (parentID.equals("0")) {
                parentStack.push("0");
                datas.addAll(rootDatas);
            } else {
                datas.addAll(findContact(parentID));
            }
            contactAdpter.notifyDataSetChanged();
        }
        
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            doBack(null);
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }
    
}
